package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @Override
    public void delete(Long id) {
        //创建构造条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //查看此表中是否有前端传来的id字段
        wrapper.eq(Dish::getCategoryId,id);
        //查询数量  若不为零
        int count = dishService.count(wrapper);
        //判断count
        if (count>0){
            throw new CustomException("当前菜品关联了其他菜品,不可以被删除");
        }
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(wrapper1);
        if (count1>0){
            throw new CustomException("当前菜品关联了其他套餐类型,不可以被删除");
        }
        removeById(id);
    }
}
